package com.vzaar.examples;

import java.io.File;
import java.util.Arrays;

import com.vzaar.UploadProgressCallback;

/**
 * Example of a upload progress callback class.
 * 
 * @author Marc G. Smith
 */
public class VideoUploadCallbackExample implements UploadProgressCallback 
{
	///////////////////////////////////////////////////////////////////////////
	// Private Members ////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////
	
	private String last = null;
	private static char[] BACKSPACE = null;
	
	static {
		// Create the backspace buffer to reset the progress output
		BACKSPACE = new char[56];
		Arrays.fill(BACKSPACE, '\b');
	}
	
	///////////////////////////////////////////////////////////////////////////
	// Public Methods /////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////

	/**
	 * The upload procedure has completed if an error hasn't occured.
	 * 
	 * @param file the file being uploaded that the error occurred on
	 * @param videoId the video id of the uploaded video 
	 */
	public void complete(File file, int videoId) {
		System.out.println("\n" + file.getName() + ": Video Id=" + videoId);
	}

	///////////////////////////////////////////////////////////////////////////

	/**
	 * An error occurred during the upload progress.
	 *  
	 * @param file the file being uploaded that the error occurred on
	 * @param error the error cause
	 */
	public void error(File file, String error) {
		System.out.println("\n" + file.getName() + ": Error - " + error);
	}

	///////////////////////////////////////////////////////////////////////////

	/**
	 * A progress update.
	 * 
	 * @param file the file being uploaded that the error occurred on
	 * @param sent the number of bytes sent so far
	 * @param length the total number of bytes to be uploaded
	 */
	public void progress(File file, long sent, long length) {
		try {
			int percent = (int)((sent * 100) / length);
			String progress = getProgressString(percent);
			if(!progress.equals(last)) {
				if(last != null) {
					System.out.print(BACKSPACE);
				}
				else {
					System.out.print("\n" + file.getName() + ": [");
				}
				
				System.out.print(progress);
				last = progress;
			}
		}
		catch(RuntimeException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	///////////////////////////////////////////////////////////////////////////

	/**
	 * Create the progress string.
	 * 
	 * @param percent the current percentage
	 * @return the progress output string.
	 */
	private String getProgressString(int percent) {
		StringBuilder buffer = new StringBuilder(56);
		int filled = percent / 2;
		int i = 0;
		for(; i < filled; ++i) buffer.append('-');
		buffer.append('>');
		for(; i < 50; ++i) buffer.append(' ');
		buffer.append("]");
		if(percent < 10) buffer.append(' ');
		if(percent < 100) buffer.append(' ');
		buffer.append(percent + "%");
		return buffer.toString();
	}

	///////////////////////////////////////////////////////////////////////////
}
